package jgcp.message;

import jgcp.common.network.Connection;
/**
 * 
 * @Date 31/05/2009
 * @author Jie Zhao (288654)
 * @version 1.0
 */
public abstract class MessageHandler extends Thread {
	protected Connection connection = null;

	public MessageHandler(Connection con) {
		connection = con;
	}

	public void run() {
		while (true) {
			try {
				byte[] data = connection.readData();
				if (data == null)
					break;
				Message msg = Message.parseMessage(data);
				processMessage(msg);
			} catch (Exception e) {
				break;
			}
		}
	}

	public void processMessage(Message m) throws Exception {
		Message msg = new Message();
		msg.setType(MessageType.UNKNOWN);
		msg.setMessageBody("Unknown Message!");
		connection.writeData(msg.toByteArray());
	}
}
